package org.example.springmessage.controller;

import org.example.springmessage.domain.Speak;
import org.example.springmessage.domain.User;
import org.example.springmessage.refult.R;
import org.example.springmessage.service.SpeakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/speak")
public class SpeakController {
    @Autowired
    private  SpeakService speakService;
    @RequestMapping("/getAllSpeaks")
    public R getAllSpeaks(String messageid){
        return R.ok(speakService.getAllSpeaks(messageid));
    }

    @PostMapping("/insert")
    public R insert(Speak speak, @AuthenticationPrincipal UserDetails user){
        String v = user.getAuthorities().toString();
        v = v.substring(1,v.length()-1);
        speak.setUserid(Integer.valueOf(v));
        return R.ok(speakService.insert(speak));
    }
}
